package com.jin.webvideo.externalInterfaces.video;

import com.jin.webvideo.exception.UnavailableServiceException;
import com.jin.webvideo.externalInterfaces.PlatformSupport;
import com.jin.webvideo.externalInterfaces.entity.Filmography;
import com.jin.webvideo.externalInterfaces.entity.FilmographyInfo;
import com.jin.webvideo.externalInterfaces.entity.ParticipationMate;
import com.jin.webvideo.util.entity.EntityUtil;
import com.jin.webvideo.util.sys.ClientUtil;
import com.jin.webvideo.util.base.StringUtil;
import com.jin.webvideo.util.base.URLUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Component
public class NetflyApi extends AbstractVideoAPI {
    private static final String DETAIL_URL = "https://www.netfly.tv/vod/detail/%s.html";
    @Override
    String platformName() {
        return PlatformSupport.PLATFORM_NETFLY;
    }

    @Override
    public Filmography baseInfo(String docid, String tvid) throws UnavailableServiceException {
        Filmography filmography = apiService.getVideoInformation(PlatformSupport.PLATFORM_NETFLY, docid, tvid);
        filmography.setVideoInfos(new ArrayList<>());
        return filmography;
    }
    @Override
    public List<FilmographyInfo> videoInfoList(String docid, String tvid, Integer pageNo, Integer pageSize) throws UnavailableServiceException {
        return baseInfoWithVideoInfoList(docid, tvid, pageNo, pageSize).getVideoInfos();
    }

    @Override
    public Filmography baseInfoWithVideoInfoList(String docid, String tvid, Integer pageNo, Integer pageSize) throws UnavailableServiceException {
        try {
            return baseInfo(docid, tvid, pageNo, pageSize);
        } catch (IOException e) {
            e.printStackTrace();
            throw new UnavailableServiceException();
        }
    }

    private Filmography baseInfo(String docid, String tvid, Integer pageNo, Integer pageSize) throws IOException {
        String docText = ClientUtil.httpGet(String.format(DETAIL_URL, docid));
        Document doc = Jsoup.parse(docText);
        Elements mains = doc.getElementsByClass("module-info-main");
        if (mains.size() < 1) {
            return null;
        }
        Filmography filmography = new Filmography();
        // Elements metas = doc.getElementsByAttribute("property");
        Elements elements = doc.select(".module-info-poster img");
        if (elements.size() > 0) {
            filmography.setImg( elements.get(0).attr("src") );
        }
        // filmography.setImg(getContentValue(metas,  "og:image"));
        filmography.setPlatformName(PlatformSupport.PLATFORM_NAME_NETFLY);
        filmography.setPlatform(PlatformSupport.PLATFORM_NETFLY);
        filmography.setId(docid);
        //filmography.setUrl(getContentValue(metas, "og:url"));
        filmography.setDisplayMode(FilmographyInfo.DISPLAY_MODE_INDEX_BLOCK);
        ParticipationMate mate = new ParticipationMate();
        /*mate.setActor(EntityUtil.string2Part(getContentValue(metas, "og:movie:actor")));
        mate.setDirector(EntityUtil.string2Part(getContentValue(metas, "og:movie:director")));*/
        filmography.setMate(mate);

        Element main = mains.get(0);
        Elements descBox = main.getElementsByClass("show-desc");
        if (descBox.size() > 0) {
            filmography.setDesc(descBox.get(0).text());
        }
        Elements items = main.getElementsByClass("module-info-item");
        mate.setDirector(EntityUtil.string2Part(getValByKey(items, "导演：").replace("/", ",")));
        mate.setDirector(EntityUtil.string2Part(getValByKey(items, "主演：").replace("/", ",")));

        filmography.setYear(getValByKey(items, "上映："));
        filmography.setVideoMate(getValByKey(items, "集数："));
        Elements name = main.getElementsByTag("h1");
        if (name.size() > 0) {
            filmography.setName(name.text());
        }
        filmography.setVideoInfos(createVideoInfo(doc, docid));
        return filmography;
    }
    private List<FilmographyInfo> createVideoInfo(Document doc, String docid) {
        Elements listContent = doc.getElementsByClass("module-play-list-content");
        Elements itemContent = findMaxContent(listContent).getElementsByClass("module-play-list-link");
        List<FilmographyInfo> infos = new ArrayList<>();
        for (int i = 0, l = itemContent.size(); i < l; i++) {
            int index = i + 1;
            Element element = itemContent.get(i);
            FilmographyInfo info = new FilmographyInfo();
            info.setId(String.valueOf(index));
            info.setPlatform(PlatformSupport.PLATFORM_NETFLY);
            String href = element.attr("href");
            href = URLUtil.resolveStr(href, String.format(DETAIL_URL, docid));
            info.setUrl(href);
            info.setNumber(index);
            info.setDisplayMode(FilmographyInfo.DISPLAY_MODE_INDEX_BLOCK);
            info.setTitle(element.attr("title"));
            infos.add(info);
        }
        return infos;
    }
    private Element findMaxContent(Elements listContent) {
        int max = -1;
        Element maxElement = null;
        for (int i = 0, l = listContent.size(); i < l; i++) {
            int count = listContent.get(i).childNodeSize();
            if (count > max) {
                max = count;
                maxElement = listContent.get(i);
            }
        }
        return maxElement;
    }
    private String getValByKey(Elements elements, String key) {
        for (int i = 0, l = elements.size(); i < l; i++) {
            Element elem = elements.get(i);
            Elements elems = elem.getElementsByClass("module-info-item-title");
            if (elems.size() < 1) {
                continue;
            }
            if (key.equals(elems.get(0).text().trim())) {
                elems = elem.getElementsByClass("module-info-item-content");
                if (elems.size() < 1) {
                    continue;
                }
                return StringUtil.eliminateWhiteSpace(elems.get(0).text());
            }
        }
        return "";
    }
    private String getContentValue(Elements elements, String value) {
        Element elem = getElementByValue(elements, "property", value);
        if (null == elem) {
            return "";
        }
        return elem.attr("content");
    }
    private Element getElementByValue(Elements elements, String key, String value) {
        for (int i = 0, l = elements.size(); i < l; i++) {
            String val = elements.get(i).attr(key);
            if (value.equals(val)) {
                return elements.get(i);
            }
        }
        return null;
    }
}
